//
// Created by 17775 on 2024/9/18.
//

#include "livemode_powerprofile_options.h"
#include "ui_LiveMode_PowerProfile_Options.h"


LiveMode_PowerProfile_Options::LiveMode_PowerProfile_Options(QWidget *parent) :
        QDockWidget(parent), ui(new Ui::LiveMode_PowerProfile_Options) {
    ui->setupUi(this);
    setWindowTitle("LiveMode:Power Profile");
    setWindowFlags(Qt::Window | Qt::WindowTitleHint | Qt::WindowCloseButtonHint);
    ui->cBox_PowerFraction->addItem("63.21%");
    ui->cBox_PowerFraction->addItem("86.47%");
    ui->cBox_PowerFraction->addItem("98.17%");
    ui->cBox_PowerFraction->addItem("98.89%");
    ui->cBox_PowerFraction->setCurrentIndex(1);
    ui->cBox_PowerFraction->setEditable(true);
    ui->cBox_PowerFraction->setInsertPolicy(QComboBox::NoInsert);

    ui->cBox_AOI->addItem("<Always Main AOI>");
    ui->cBox_Methods->addItem("Peak");
    ui->cBox_Methods->addItem("Centroid");
    ui->cBox_Methods->addItem("2nd Moments");
    ui->cBox_Methods->addItem("BeamWidth");
    ui->cBox_Methods->addItem("Max. Power");
    ui->cBox_Methods->addItem("Local Maximum");

    initSetting();

    connect(ui->rBtn_Manually, &QRadioButton::clicked, this, [=]() {
        isSettingPowerFraction = false;
        isSettingPowerPosition = true;
        bool click = ui->rBtn_Manually->isChecked();
        if(click) {
            ui->cBox_PowerFraction->setEnabled(false);
            ui->dSBox_Manually->setEnabled(true);
        }
        bool ok = false;
        auto pos = ui->dSBox_Manually->text().toDouble(&ok);
        if(ok && click) {
            emit setCursorPosition(pos);
        }
    });

    connect(ui->dSBox_Manually, &QDoubleSpinBox::valueChanged, this, [=]() {
        if (isSettingPowerFraction) return;
        isSettingPowerPosition = true;
        bool ok = false;
        auto pos = ui->dSBox_Manually->text().toDouble(&ok);
        if(ok) {
            emit setCursorPosition(pos);
        }
    });

    connect(ui->rBtn_PowerFraction, &QRadioButton::clicked, this, [=]() {
        isSettingPowerPosition = false;
        isSettingPowerFraction = true;
        bool click = ui->rBtn_PowerFraction->isChecked();
        if(click) {
            ui->dSBox_Manually->setEnabled(false);
            ui->cBox_PowerFraction->setEnabled(true);
        }
        auto currentText = ui->cBox_PowerFraction->currentText();
        if(!currentText.isEmpty() && currentText.endsWith("%")) {
            currentText.chop(1);
            auto value = currentText.toDouble();
            emit setPowerFraction(value, click);
        }
    });

    connect(ui->cBox_PowerFraction, &QComboBox::currentTextChanged, this, [=]() {
        if (isSettingPowerPosition) return;
        isSettingPowerFraction = true;
        bool click = ui->rBtn_PowerFraction->isChecked();
        auto currentText = ui->cBox_PowerFraction->currentText();
        if(currentText.isEmpty()) return;
        if(currentText.endsWith("%")) currentText.chop(1);
        auto value = currentText.toDouble();
        emit setPowerFraction(value, click);
    });

    lineEdit = ui->cBox_PowerFraction->lineEdit();
    connect(lineEdit, &QLineEdit::returnPressed, [=]() {
        QString text = ui->cBox_PowerFraction->currentText();
        if (!text.isEmpty() && !text.endsWith("%")) {
            bool isNumeric = false;
            text.toDouble(&isNumeric);
            if (isNumeric) {
                ui->cBox_PowerFraction->setEditText(text + "%");
            }
        }
    });

    connect(ui->dSBox_X, &QDoubleSpinBox::valueChanged, this, [=]() {
        bool ok = false;
        auto xRatio = ui->dSBox_X->text().toDouble(&ok);
        auto yRatio = ui->dSBox_Y->text().toDouble(&ok);
        if(ok) {
            emit setAxisRatio(xRatio, yRatio);
        }
    });

    connect(ui->dSBox_X0, &QDoubleSpinBox::valueChanged, this, [=](double value) {
        auto x = ui->dSBox_X0->value();
        auto y = ui->dSBox_Y0->value();
        emit set2DViewCenterPos(x, y);
    });

    connect(ui->dSBox_Y0, &QDoubleSpinBox::valueChanged, this, [=](double value) {
        auto x = ui->dSBox_X0->value();
        auto y = ui->dSBox_Y0->value();
        emit set2DViewCenterPos(x, y);
    });

    connect(ui->cBox_Activate, &QCheckBox::clicked, this, [=]() {
        auto clicked = ui->cBox_Activate->isChecked();
        emit setActivate(clicked);
    });

    connect(ui->cBox_AOI, &QComboBox::currentIndexChanged, this, [=]() {

    });

    connect(ui->cBox_Methods, &QComboBox::currentIndexChanged, this, [=](){
        auto index = ui->cBox_Methods->currentIndex();
        emit setMethod(index);
    });

    connect(ui->cBox_Abscissa, &QCheckBox::clicked, this, [=]() {
        auto clicked = ui->cBox_Abscissa->isChecked();
        emit showAbscissa(clicked);
    });

    connect(ui->cBox_Ordinate, &QCheckBox::clicked, this, [=]() {
        auto clicked = ui->cBox_Ordinate->isChecked();
        emit showOrdinate(clicked);
    });
}

void LiveMode_PowerProfile_Options::initSetting() {
    cursorGroup = new QButtonGroup(this);
    cursorGroup->addButton(ui->rBtn_Manually);
    cursorGroup->addButton(ui->rBtn_PowerFraction);

    ui->cBox_PowerFraction->setCurrentIndex(1);
    ui->rBtn_Manually->setChecked(true);
    ui->rBtn_DataRange->setChecked(true);
//    ui->cBox_Abscissa->setChecked(true);
//    ui->cBox_Ordinate->setChecked(true);
}

void LiveMode_PowerProfile_Options::setStdCS(double x_center, double y_center) {
    ui->dSBox_X0->setValue(x_center);
    ui->dSBox_Y0->setValue(y_center);
}

void LiveMode_PowerProfile_Options::setManual(double factor) {
//    auto mValue = ui->dSBox_Manually->value();
    ui->dSBox_Manually->setValue(factor);
}

double LiveMode_PowerProfile_Options::getRatio() {
    auto a = ui->dSBox_Manually->value();
    auto b = ui->dSBox_X0->value();
    return a / b;
}

void LiveMode_PowerProfile_Options::changeUnit(QString unit) {
    ui->cursorUnit->setText(unit);
    ui->Label_StdCS_X->setText(unit);
    ui->Label_StdCS_Y->setText(unit);
}

void LiveMode_PowerProfile_Options::setStepByUnit(QString unit, double sizeX, double sizeY) {
    auto xStep = conversion(unit, sizeX);
    auto yStep = conversion(unit, sizeY);
    auto mStep = conversion(unit, 2 * sizeX, 2);
    ui->dSBox_X0->setSingleStep(xStep);
    ui->dSBox_Y0->setSingleStep(yStep);
    ui->dSBox_Manually->setSingleStep(mStep);
}

double LiveMode_PowerProfile_Options::conversion(QString unit, double size , int a) {
//    const std::unordered_map<QString, double> conversionStep {
//            {"px", a},
//            {"um", size},
//            {"mm", size / 1000.0},
//            {"cm", size / 10000.0},
//            {"in", size * 0.3937 / 10000.0}
//    };
//    const auto it = conversionStep.find(unit);
//    return (it != conversionStep.end()) ? it->second : 1.0 / 1000000.0;
//    double step = 1.0;
//    if (conversionStep.find(unit) != conversionStep.end()) {
//        step = conversionStep.at(unit);
//    } else {
//        step = size / 1000000.0;
//    }
//    return step;

    const std::unordered_map<QString, double> conversionStep {
            {"px", a},
            {"um", size},
            {"mm", size / 1000.0},
            {"cm", size / 10000.0},
            {"in", size * 0.3937 / 10000.0}
    };
    const auto it = conversionStep.find(unit);
    return (it != conversionStep.end()) ? it->second : 1.0 / 1000000.0;
}

double LiveMode_PowerProfile_Options::unitConversion(const QString& unit, double size) {
    const std::unordered_map<QString, double> conversionStep {
            {"px", 1.0},
            {"um", 1 / size},
            {"mm", 1000.0 / size},
            {"cm", 10000.0 / size},
            {"in", 10000.0 / 0.3937 / size}
    };
    double factor = 1.0;
    if (conversionStep.find(unit) != conversionStep.end()) {
        factor = conversionStep.at(unit);
    } else {
        factor = 1000000.0 / size;
    }
    return factor;
}

void LiveMode_PowerProfile_Options::setFractionValue(bool bPosition, double value) {
    bPosition ? ui->dSBox_Manually->setValue(value) : ui->cBox_PowerFraction->setCurrentText(QString::number(value));
//    ui->dSBox_Manually->setValue(value);
}

QString LiveMode_PowerProfile_Options::getCurrentPosition() {
    return ui->dSBox_Manually->text();
}

QString LiveMode_PowerProfile_Options::getCurrentFraction() {
    return ui->cBox_PowerFraction->currentText();
}

LiveMode_PowerProfile_Options::~LiveMode_PowerProfile_Options() {
    delete ui;
}
